package demo01Singleton;

import org.junit.Test;

/**
 * @author: WangPeng
 * @createTime: 2022/11/07 18:01
 * @description:
 * @version: v1.0
 */
public class Test01 {

    @Test
    public void test1() {
        SingletonHungryType singleton1 = SingletonHungryType.getSingleton();
        SingletonHungryType singleton2 = SingletonHungryType.getSingleton();
        SingletonHungryType singleton3 = SingletonHungryType.getSingleton();

        System.out.println("singleton2 = " + singleton2);
        System.out.println("singleton1 = " + singleton1);
        System.out.println("singleton3 = " + singleton3);
        System.out.println();
        singleton1.show();
    }

    @Test
    public void test2() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(SingletonLazyType.getSingleton());
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(SingletonLazyType.getSingleton());
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(SingletonLazyType.getSingleton());
            }
        }).start();
    }

    @Test
    public void test3() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(SingletonLazyTypeLock.getSingleton());
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(SingletonLazyTypeLock.getSingleton());
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(SingletonLazyTypeLock.getSingleton());
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(SingletonLazyTypeLock.getSingleton());
            }
        }).start();
    }

    @Test
    public void test4() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(SingletonLazyTypeDoubleLock.getSingleton());
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(SingletonLazyTypeDoubleLock.getSingleton());
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(SingletonLazyTypeDoubleLock.getSingleton());
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(SingletonLazyTypeDoubleLock.getSingleton());
            }
        }).start();
    }
}
